package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.bean.BaseUser;
import com.hhs.storage.bean.InputParam;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.bean.Suggestion;
import com.hhs.storage.dao.SuggestionDao;
import com.hhs.storage.service.BaseUserService;
import com.hhs.storage.service.SuggestionService;
import com.hhs.storage.util.SysUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-04-22
 */
@Service
@Transactional
public class SuggestionServiceImpl extends ServiceImpl<SuggestionDao, Suggestion> implements SuggestionService {
    @Resource
    private BaseUserService userService;
    @Resource
    private HttpServletRequest request;
    /**
     *
     * @param suggestion
     * @param inputParam page size 分页
     * @param inputParam mainId 要查询的部门id
     * @param inputParam createTimeStartTime createTimeEndTime 根据创建时间的开始日期，结束日期查询
     * @return
     */
    @Override
    public Map<String, Object> getAll(Suggestion suggestion, InputParam inputParam) {
        QueryWrapper<Suggestion> qw = new QueryWrapper<>();
        //根据日期查询
        if (inputParam.getCreateTimeStartTime()!=null&&inputParam.getCreateTimeEndTime()!=null){
            qw.between("create_time",inputParam.getCreateTimeStartTime().atStartOfDay(),
                    LocalDateTime.of(inputParam.getCreateTimeEndTime(), LocalTime.MAX));
        }
        //根据部门查询
        String departmentIds=inputParam.getMainId();
        if (StringUtils.isNotEmpty(departmentIds)){
            String[] ids=departmentIds.split(",");
            List<String> idList = new ArrayList<>();
            for (int i = 0; i < ids.length; i++) {
                idList.add("'" + ids[i] + "'");
            }
            departmentIds = idList.stream().map(String::valueOf).collect(Collectors.joining(","));
            qw.inSql("user_id","select DISTINCT  user_id from base_department_user where depart_id in (" + departmentIds + ")");
        }
        qw.orderByDesc("create_time");//按时间倒序排序
        //分页查询
        Page<Suggestion> result = this.page(new Page<Suggestion>(
                inputParam.getPage(),
                inputParam.getSize()), qw);
        List<Suggestion> list = result.getRecords();
        //设置部门和用户信息
        for (int i = 0; i <list.size() ; i++) {
            Suggestion temp=list.get(i);
            BaseUser user=userService.getById(temp.getUserId());
            temp.setUser(user);
        }
        long total = result.getTotal();
        Map map = new HashMap();
        map.put("code", 1);
        map.put("rows", list);
        map.put("total", total);
        return map;
    }

    @Override
    public Map<String, Object> addOrUpdate(Suggestion suggestion) {
        if (StringUtils.isEmpty(suggestion.getContent())) {
            return ResponseInfo.error("内容不可为空");
        }
        //当前登录用户id
        String loginUserId = SysUtil.getLoginUserId(request);
        if (suggestion.getId()==null) {
            //非超级管理员，权限校验
            //新增
            suggestion.setUserId(loginUserId);
            suggestion.setCreateTime(LocalDateTime.now());
            this.save(suggestion);
            return ResponseInfo.success("新增成功");
        } else {
            //编辑
            //更新数据
            UpdateWrapper uw = new UpdateWrapper();
            uw.set("content", suggestion.getContent());
            uw.eq("id", suggestion.getId());
            this.update(uw);
            return ResponseInfo.success("编辑成功");
        }
    }

    @Override
    public Map<String, Object> dele(String id) {
        this.removeById(id);
        return ResponseInfo.success("删除成功");
    }
}
